class pcSelector extends eui.Component{
    private return:eui.Rect;
    private close:eui.Image;

    private active:eui.Rect;
    private pro:eui.Label;
    private city:eui.Label;
    private area:eui.Label;

    private _pro:string = "";
    private _city:string = "";
    private _area:string = "";

    private collection:eui.ArrayCollection;
    private list:eui.List;
    
    private _proData:Array<any> = [];
    private _cityData:Array<any> = [];
    private _areaData:Array<any> = [];

    private _index = 0;//province:0,city:1,area:2

    private _color = [0xFF6D2B,0x000000];
    public constructor(pro:string = "",city:string = "",area:string = ""){
        super();
        this._pro = pro;
        this._city = city;
        this._area = area;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "pcSelectorSkin"; 
    }
    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.init();
    }
    private init():void{
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this);
        this.close.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this);

        if(this._pro !== "" && this._city !== "" && this._area !== ""){
            this.pro.text = this._pro;
            this.city.text = this._city;
            this.area.text = this._area;
            this.city.visible = true;
            this.area.visible = true;
            this.pro.textColor = this._color[1];
            this.city.textColor = this._color[1];
            this.area.textColor = this._color[0];
            this._index = 2;
            setTimeout(()=>{
                egret.Tween.get(this.active).to({x:this.area.x,width:this.area.width + 30},400);
            },200);
        }
        if(this._pro !== "" && this._city !== "" && this._area == ""){
            this.pro.text = this._pro;
            this.city.text = this._city;
            this.city.visible = true;
            this.pro.textColor = this._color[1];
            this.city.textColor = this._color[0];
            this.area.textColor = this._color[1];
            this._index = 1;
            setTimeout(()=>{
                egret.Tween.get(this.active).to({x:this.city.x,width:this.area.width + 30},400);
            },200);
        }
        if(this._pro !== "" && this._city == "" && this._area == ""){
            this.pro.text = this._pro;
        }

        this.pro.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.refresh(this._proData);
            this._index = 0;
            egret.Tween.get(this.active).to({x:0,width:this.pro.width + 30},200);
            this.pro.textColor = this._color[0];
            this.city.textColor = this._color[1];
            this.area.textColor = this._color[1];
        },this);
        this.city.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.refresh(this._cityData);
            this._index = 1;
            egret.Tween.get(this.active).to({x:this.city.x,width:this.city.width + 30},200);
            this.pro.textColor = this._color[1];
            this.city.textColor = this._color[0];
            this.area.textColor = this._color[1];
        },this);
        this.area.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.refresh(this._areaData);
            this._index = 2;
            egret.Tween.get(this.active).to({x:this.area.x,width:this.area.width + 30},200);
            this.pro.textColor = this._color[1];
            this.city.textColor = this._color[1];
            this.area.textColor = this._color[0];
        },this);

        this.collection = new eui.ArrayCollection([]);
        this.list.dataProvider = this.collection;
        this.list.itemRenderer = toRollIR;

        var move = "";
        this.list.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            move = "";
        },this);
        this.list.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            move = 'move';
        },this);
        this.list.addEventListener(eui.ItemTapEvent.ITEM_TAP,(evt:egret.Event)=>{
            if(move == "move") return;
            var dataList:eui.List = evt.currentTarget;
            var item = dataList.selectedItem;
            var index = dataList.selectedIndex;
            switch (this._index){
                case 0:{;
                    if(this._city !== ""){
                        this.city.text = "请选择";
                        this.area.visible = false;
                        for(let i = 0,lg = this._cityData.length;i < lg;i ++){
                            this._cityData[i].active = 0;
                        }
                    }else{
                        this.city.visible = true;
                    }
                    this._proData = this.setActive(this._proData,index);
                    this.pro.text = item.label;
                    
                    if(this._pro == item.label){
                        this.refresh(this._cityData);
                    }else{
                        this._pro = item.label;
                        this.getCity(item.index);
                    }
                    this._index = 1;
                    setTimeout(()=>{
                        egret.Tween.get(this.active).to({x:this.city.x},200);
                    },100);
                    this.pro.textColor = this._color[1];
                    this.city.textColor = this._color[0];
                    this.area.textColor = this._color[1];
                    break;
                }
                case 1:{
                    this.area.visible = true;
                    this._cityData = this.setActive(this._cityData,index);
                    this.city.text = item.label;
                    this.area.text = "请选择";
                    if(this._city == item.label){
                        if(this._area !== ""){
                            for(let i = 0,lg = this._areaData.length;i < lg;i ++){
                                this._areaData[i].active = 0;
                            }
                        }
                        this.refresh(this._areaData);
                    }else{
                        this._city = item.label;
                        this.getArea(index);
                    }
                    this._index = 2;
                    setTimeout(()=>{
                        egret.Tween.get(this.active).to({x:this.area.x},200);
                    },100);
                    this.pro.textColor = this._color[1];
                    this.city.textColor = this._color[1];
                    this.area.textColor = this._color[0];
                    break;
                }
                case 2:{
                    this._areaData = this.setActive(this._areaData,index);
                    this.area.text = item.label;
                    this._area = item.label;
                    this.dispatch();
                    break;
                }
            }
        },this);
        this.getProvince();
    }
    private getProvince():void{
        for(let i in json.area){
            let obj:any = {};
            obj.index = i;
            obj.label = json.area[i].name;
            if(obj.label == this._pro){
                obj.active = 1;
                if(this._city !== ""){
                    setTimeout(()=>{
                        this.getCity(obj.index);
                    },200);
                }
            }else{
                obj.active = 0;
            }
            this._proData.push(obj);
        }
        this.refresh(this._proData);
    }
    private getCity(index):void{
        this._cityData = [];
        var obj = json.area[index].child;
        if(obj.length == 0){
            this._city = "";
            this._area = "";
            this.dispatch();
        }
        let n = 0;
        for(let i in obj){
            let tr:any = {};
            tr.index = i;
            tr.label = obj[i].name;
            if(tr.label == this._city){
                tr.active = 1;
                let q = n;
                if(this._area !== ""){
                    setTimeout(()=>{
                        this.getArea(q);
                    },200);
                }
            }else{
                tr.active = 0;
            }
            n = n + 1;
            tr.child = obj[i].child;
            this._cityData.push(tr);
        }
        this.refresh(this._cityData);
    }
    private getArea(index):void{
        this._areaData = [];
        var obj = this._cityData[index].child;
        if(obj.length == 0){
            this._area = "";
            this.dispatch();
        }
        for(let i in obj){
            let tr:any = {};
            tr.index = i;
            tr.label = obj[i];
            if(tr.label == this._area){
                tr.active = 1;
                this._index = 2;
            }else{
                tr.active = 0;
            }
            this._areaData.push(tr);
        }
        this.refresh(this._areaData);
    }
    private setActive(item,index):any{
        for(var i = 0,lg = item.length;i < lg;i ++){
            if(index !== i){
                item[i].active = 0;
            }else{
                item[i].active = 1;
            }
        }
        return item;
    }
    private refresh(data):void{
        var arr = [];
        for(var i = 0,lg = data.length;i < lg;i ++){
            let obj = data[i];
            arr.push(obj);
        }
        this.collection.replaceAll(arr);
        this.collection.refresh();
    }
    private dispatch():void{
        let dataPost = {
            pro:this._pro,
            city:this._city,
            area:this._area
        }
        this.dispatchEventWith("address",true,dataPost);
        this.parent.removeChild(this);
    }
}
class toRollIR extends eui.ItemRenderer{
    private active:eui.Image;
    private label:eui.Label;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "pcSelectorIRSkin"; 
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        // this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
        //     console.log(this.data);
        //     this.parent.dispatchEventWith("click",true,this.data);
        // },this);
    }
    protected dataChanged():void{
        if(this.data.active == 0){
            this.active.visible = false;
            this.label.textColor = 0x000000;
        }else{
            this.active.visible = true;
            this.label.textColor = 0xff7900;
        }
    }
}